﻿package com.asgamer.directionalmovement 
{
	
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	
	public class Laser extends MovieClip
	{
		
		private var vx:Number = 0;
		private var vy:Number = 0;
		private var speed:Number = 12;
		
		public function Laser (x:Number, y:Number, shipRotation:Number) : void
		{

			this.rotation = shipRotation;
			vy += Math.sin(degreesToRadians(shipRotation)) * speed;
			vx += Math.cos(degreesToRadians(shipRotation)) * speed;

			this.x = x +vx*2;
			this.y = y + vy*2;
			
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
		}
		
		public function loop(e:Event) : void
		{
			y += vy;
			x += vx;
			
			if (x > 520 || y > 420 || x < -20 || y < -20)
				destroy();
		}
		
		public function destroy() : void
		{
			if (parent)
				parent.removeChild(this);
			
			removeEventListener(Event.ENTER_FRAME, loop);
		}
		
		public function degreesToRadians(degrees:Number) : Number
		{
			return degrees * Math.PI / 180;
		}
		
	}
	
	
}